package com.sdut.material.service.impl;

import com.sdut.material.pojo.Material;
import com.sdut.material.mapper.MaterialMapper;
import com.sdut.material.pojo.Type;
import com.sdut.material.pojo.vo.MaterialVO;
import com.sdut.material.service.IMaterialService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sdut.material.service.ITypeService;
import com.sdut.material.util.JSONResult;
import com.sdut.material.util.LayUITableResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Yu
 * @since 2022-04-20
 */
@Service
public class MaterialServiceImpl extends ServiceImpl<MaterialMapper, Material> implements IMaterialService {
    @Autowired
    private MaterialMapper materialMapper;
    @Autowired
    private ITypeService typeService;
    @Override
    public Material selectById(Integer materialId) {
        return materialMapper.selectById(materialId);
    }

    @Override
    public LayUITableResult selectByPage(long page, long limit) {
        long offset = (page - 1) * limit;
        List<Material> materialList = materialMapper.selectByPage(offset,limit);
        List<MaterialVO> materialVOList = new ArrayList<>();
        for (Material material : materialList) {
            MaterialVO materialVO = new MaterialVO();
            Type type = typeService.selectById(material.getTypeId());
            materialVO.setId(material.getId());
            materialVO.setName(material.getName());
            materialVO.setNorms(material.getNorms());
            materialVO.setTypeName(type.getName());
            materialVOList.add(materialVO);
        }
        int totalCount = materialMapper.selectTotalCount();
        return LayUITableResult.ok((long)totalCount,materialVOList);
    }

    @Override
    public JSONResult insert(Material material, String types) {
        Material materialInsert = new Material();
        materialInsert.setName(material.getName());
        materialInsert.setNorms(material.getNorms());
        String[] typeId = types.split(",");
        materialInsert.setTypeId(Integer.parseInt(typeId[0]));
        materialMapper.insert(materialInsert);
        return JSONResult.ok("添加成功");
    }

    @Override
    public JSONResult selectMaterialAndTypes(Integer materialId) {
        Map<String,Object> map = new HashMap<>();
        Material material = materialMapper.selectById(materialId);
        map.put("material",material);
        JSONResult jsonResult = typeService.selectAllTypes();
        map.put("allTypes",jsonResult.getData());
        List list = new ArrayList();
        list.add(material.getTypeId());
        map.put("selectIds",list);
        return JSONResult.ok(map);
    }

    @Override
    public JSONResult updateMaterial(Material material, String types) {
        String[] typeIds = types.split(",");
        material.setTypeId(Integer.parseInt(typeIds[0]));
        materialMapper.updateById(material);
        return JSONResult.ok("修改成功");
    }

    @Override
    public void deleteById(Integer materialId) {
        materialMapper.deleteById(materialId);
    }
}
